package com.clj.blesample.compose;

import com.clj.blesample.entity.ErrorResult;
import com.clj.blesample.entity.Result;
import com.clj.blesample.http.exception.RequestFailedException;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;

/**
 * Created by jiajia on 2018/9/29.
 */

public class EmptyTransformer<T extends Result> implements ObservableTransformer<T, T> {
    @Override
    public ObservableSource<T> apply(Observable<T> upstream) {
        return upstream
                .map(tResult -> {
                    if (tResult.code == 200) {
                        return tResult;
                    } else {
                        throw new RequestFailedException(new ErrorResult(tResult.code, tResult.info));
                    }
                });
    }
}
